<?php
class DetailsController extends  IndexController 
{
	public function init()
    {
		parent::init();
        $this->utl            = Yaf_Registry::get("utl");
        $this->DetailsModel   = new DetailsModel();
        $this->UserModel      = new UserModel();
        $this->WebsiteModel   = new WebsiteModel();
    }





    public function listAction()
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
        }
        $allCounts         = 0;
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $pushUserAboutInfo = array();
        $user_info         = $this->UserModel->getUserInfo();
        $site_info         = $this->WebsiteModel->selectSites();
        $pushUser          = $this->DetailsModel->selectPushUser();
        $pushSites         = $this->DetailsModel->selectPushSites();
        $pushTime          = $this->DetailsModel->selectPushTime();
        
        if(isset($post) == false ){
           // echo "000";
            $pushUserInfo     = $this->DetailsModel->selectPushToday($pushUser,$pushSites,$pushTime);
           // $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        }
        else if(isset($post) == false  || $post['date'] == "" && $post['user_id'] == "0"){
          // echo "555";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');   
        }
        else if(isset($post['date']) == true  && $post['date'] == "" &&  $post['user_id'] == ""){
           // echo "444";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        }
        else if(isset($post['date']) == true  && $post['date'] != "" && $post['user_id'] == "0"){
            //echo "333";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$post['date']);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        }

        else if(isset($post['user_id']) == true  && $post['user_id'] != ""  && $post['date'] == ""){
            //echo "222";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,(int)$post['user_id'],$pushTime,$post['date']);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        
        }

        else if(isset($post['user_id']) == true  && $post['user_id'] != "0" && $post['date'] != ""){
            //echo "111";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAndTime($pushUser,$pushSites,(int)$post['user_id'],$post['date'],$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        
        }

        foreach ($pushUserInfo as $k => $v) {
            if( ((int)$page-1)*(int)$offset <=$k && $k < (int)$page*(int)$offset){
                if(isset($post) == false){
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);
                    $pushUserAboutInfo[$k]['page']       = $allpage;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                }
               else if(isset($post) == false || $post['date'] == "" || $post['user_id'] == ""){
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);
                    $pushUserAboutInfo[$k]['page']       = $allpage;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                }
                
                else if(isset($post['date']) == true  && $post['date'] != "" && $post['date'] == date('Y-m-d', (int) $v[0]['create_time'])){
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);
                    $pushUserAboutInfo[$k]['page']       = $allpage;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                } 

                else if(isset($post['user_id']) == true  && $post['user_id'] != "" ){
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);
                    $pushUserAboutInfo[$k]['page']       = $allpage;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                } 
                else if(isset($post['user_id']) == true  && $post['user_id'] != "0" && $post['date'] != ""){
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);
                    $pushUserAboutInfo[$k]['page']       = $allpage;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                }
            }      
        }
        if(!empty($post)){
            exit(json_encode(array_reverse($pushUserAboutInfo), true));
        }
        else{
            $this->getView()->assign("pushUserAboutInfo",array_reverse($pushUserAboutInfo));
            $this->getView()->assign("user_info",$user_info);
            $this->getView()->assign("allpage",$allpage);
        }

        
    }

    public function pageAction()
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
        }
        $allCounts         = 0;
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $pushUserAboutInfo = array();
        $user_info         = $this->UserModel->getUserInfo();
        $site_info         = $this->WebsiteModel->selectSites();
        $pushUser          = $this->DetailsModel->selectPushUser();
        $pushSites         = $this->DetailsModel->selectPushSites();
        $pushTime          = $this->DetailsModel->selectPushTime();
         if(isset($post) == false  || $post['date'] == "" && $post['user_id'] == "0"){
           //echo "222";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        
        }
        else if(isset($post['date']) == true  && $post['date'] != "" && $post['user_id'] == "0"){
            //echo "11";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$post['date']);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        
        }
        else if(isset($post['user_id']) == true  && $post['user_id'] != ""){
           // echo "110";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,(int)$post['user_id'],$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        
        }
        else if(isset($post['user_id']) == true  && $post['user_id'] != "0" && $post['date'] != ""){
            //echo "111";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAndTime($pushUser,$pushSites,(int)$post['user_id'],$post['date'],$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        
        }
        foreach ($pushUserInfo as $k => $v) {

            if( ((int)$page-1)*(int)$offset <=$k && $k < (int)$page*(int)$offset){
                if(isset($post['date']) == false || $post['date'] == ""){
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                }
                else if(isset($post) == true  && $post['date'] != "" && $post['date'] == date('Y-m-d', (int) $v[0]['create_time'])){
                   // echo "1";
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);
                    $pushUserAboutInfo[$k]['page']       = $allpage;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                } 

                else if(isset($post['user_id']) == true  && $post['user_id'] != "" ){
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);
                    $pushUserAboutInfo[$k]['page']       = $allpage;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                } 
                else if(isset($post['user_id']) == true  && $post['user_id'] != "0" && $post['date'] != ""){
                    $pushUserAboutInfo[$k]               = $v;
                    $pushUserAboutInfo[$k]['rows']       = $allCounts;
                    $pushUserAboutInfo[$k]['every_page'] = $offset;
                    $pushUserAboutInfo[$k]['push_time']  = date('Y-m-d', (int) $v[0]['create_time']);
                    $pushUserAboutInfo[$k]['page']       = $allpage;
                    foreach ($user_info  AS $ks => $vs)
                    {
                        if ($v[0]['user_id'] === $vs['user_id'])
                        {
                            $pushUserAboutInfo[$k]['user_name'] = $vs['realname'];break;
                        }
                    }
                    foreach ($site_info  AS $ks => $vs)
                    {
                        if ($v[0]['siteid'] === $vs['sid'])
                        {
                            $pushUserAboutInfo[$k]['site_name'] = $vs['name'];break;
                        }
                    }
                } 
            }
        }
        //var_dump($pushUserAboutInfo);
        $this->getView()->assign("pushUserAboutInfo",array_reverse($pushUserAboutInfo));
        exit(json_encode(array_reverse($pushUserAboutInfo), true));
    }

    public function currentPageAction()
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
        }
        $allCounts         = 0;
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $pushUserAboutInfo = array();
        $user_info         = $this->UserModel->getUserInfo();
        $site_info         = $this->WebsiteModel->selectSites();
        $pushUser          = $this->DetailsModel->selectPushUser();
        $pushSites         = $this->DetailsModel->selectPushSites();
        $pushTime          = $this->DetailsModel->selectPushTime();
        if(isset($post) == false  || $post['date'] == "" && $post['user_id'] == "0"){
          // echo "555";
        $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$pushTime);
        $allCounts         = count($pushUserInfo);
        $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        }
        else if(isset($post['date']) == true  && $post['date'] == "" &&  $post['user_id'] == ""){
            //echo "444";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        }
        else if(isset($post['date']) == true  && $post['date'] != "" && $post['user_id'] == "0"){
            //echo "333";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,$post['date']);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        }

        else if(isset($post['user_id']) == true  && $post['user_id'] != ""  && $post['date'] == ""){
            //echo "222";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAboutInfo($pushUser,$pushSites,(int)$post['user_id'],$pushTime,$post['date']);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        }

        else if(isset($post['user_id']) == true  && $post['user_id'] != "0" && $post['date'] != ""){
            //echo "111";
            $pushUserInfo      = $this->DetailsModel->selectPushUserAndTime($pushUser,$pushSites,(int)$post['user_id'],$post['date'],$pushTime);
            $allCounts         = count($pushUserInfo);
            $allpage           = $this->utl::page('/Details/list', $page, $allCounts, $offset, $status='');
        }
        exit($allpage);
    }
}
?>